<?php
/**
 * Bake Template for Controller action generation.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       Cake.Console.Templates.default.actions
 * @since         CakePHP(tm) v 1.3
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
?>
	public function <?php echo $admin ?>index() {
		$this-><?php echo $currentModelName ?>->recursive = 0;
		$<?php echo $pluralName ?> = $this->paginate();
		$this->set(compact('<?php echo $pluralName ?>'));
	}

	public function <?php echo $admin ?>view($id = null) {
		$this-><?php echo $currentModelName; ?>->id = $id;
		if (!$this-><?php echo $currentModelName; ?>->exists()) {
			$this->setUserInfo(__("Invalid <?php echo strtolower($singularHumanName); ?>."), "error");
			$this->redirect( $this->referer() );
		}
		$this->set('<?php echo $singularName; ?>', $this-><?php echo $currentModelName; ?>->read(null, $id));
	}

<?php $compact = array(); ?>
	public function <?php echo $admin ?>add() {
		if ($this->request->is('post')) {
			try {
				// begin transaction
				$this-><?php echo $currentModelName; ?>->getDataSource()->begin();
				$this-><?php echo $currentModelName; ?>->create();
				if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) {
					$this->setUserInfo( __("The <?php echo strtolower($singularHumanName); ?> has been saved."), "success" );
					$this->redirect(array('action' => 'index'));
				}
				else {
					throw new Exception( __("The <?php echo strtolower($singularHumanName); ?> could not be saved.") );
				}
			}
			catch(Exception $ex) {
				// rollback transaction
				$this-><?php echo $currentModelName; ?>->getDataSource()->rollback();
				// handle the exception
				$this->log("Error saving <?php echo strtolower($singularHumanName); ?>: " . $ex->getMessage());
				$this->setUserInfo( __("The <?php echo strtolower($singularHumanName); ?> could not be saved: ") . $ex->getMessage(), 'error');
			}
		}
		
<?php
	foreach (array('belongsTo', 'hasAndBelongsToMany') as $assoc):
		foreach ($modelObj->{$assoc} as $associationName => $relation):
			if (!empty($associationName)):
				$otherModelName = $this->_modelName($associationName);
				$otherPluralName = $this->_pluralName($associationName);
				echo "\t\t\${$otherPluralName} = \$this->{$currentModelName}->{$otherModelName}->find('list');\n";
				$compact[] = "'{$otherPluralName}'";
			endif;
		endforeach;
	endforeach;
	if (!empty($compact)):
		echo "\t\t\$this->set(compact(".join(', ', $compact)."));\n";
	endif;
?>
	}

<?php $compact = array(); ?>

	public function <?php echo $admin; ?>edit($id = null) {
	
		$this-><?php echo $currentModelName; ?>->id = $id;
		
		if (!$this-><?php echo $currentModelName; ?>->exists()) {
			$this->setUserInfo(__("Invalid <?php echo strtolower($singularHumanName); ?>."), "error");
			$this->redirect( $this->referer() );
		}
		
		if ($this->request->is('post') || $this->request->is('put')) {
			try {
				// begin transaction
				$this-><?php echo $currentModelName; ?>->getDataSource()->begin();
				if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) {
					$this->setUserInfo( __("The <?php echo strtolower($singularHumanName); ?> has been saved."), "success" );
					$this->redirect(array('action' => 'view', $this-><?php echo $currentModelName; ?>->id));
				}
				else {
					throw new Exception( __("The <?php echo strtolower($singularHumanName); ?> could not be saved.") );
				}
			}
			catch(Exception $ex) {
				// rollback transaction and handle the exception
				$this-><?php echo $currentModelName; ?>->getDataSource()->rollback();
				$this->log("Error saving <?php echo strtolower($singularHumanName); ?>: " . $ex->getMessage());
				$this->setUserInfo( __("The <?php echo strtolower($singularHumanName); ?> could not be saved: ") . $ex->getMessage(), 'error');
			}
		}
		else {
			$this->request->data = $this-><?php echo $currentModelName; ?>->read(null, $id);
		}
		
<?php
		foreach (array('belongsTo', 'hasAndBelongsToMany') as $assoc):
			foreach ($modelObj->{$assoc} as $associationName => $relation):
				if (!empty($associationName)):
					$otherModelName = $this->_modelName($associationName);
					$otherPluralName = $this->_pluralName($associationName);
					echo "\t\t\${$otherPluralName} = \$this->{$currentModelName}->{$otherModelName}->find('list');\n";
					$compact[] = "'{$otherPluralName}'";
				endif;
			endforeach;
		endforeach;
		if (!empty($compact)):
			echo "\t\t\$this->set(compact(".join(', ', $compact)."));\n";
		endif;
	?>
	}

	public function <?php echo $admin; ?>delete($id = null) {
		
		$this-><?php echo $currentModelName; ?>->id = $id;
		
		if (!$this-><?php echo $currentModelName; ?>->exists()) {
			$this->setUserInfo(__("Invalid <?php echo strtolower($singularHumanName); ?>."), "error");
			$this->redirect( $this->referer() );
		}
		
		if ($this-><?php echo $currentModelName; ?>->delete()) {
			$this->setUserInfo( __("<?php echo ucfirst(strtolower($singularHumanName)); ?> deleted."), 'success' );
			$this->redirect(array('action' => 'index'));
		}
		
		$this->setUserInfo( __("<?php echo ucfirst(strtolower($singularHumanName)); ?> was not deleted."), 'error' );
		$this->redirect(array('action' => 'index'));
	}
	